package com.example.myapplication.loginandregister;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Toast;

import com.example.database.entity.UserEntity;
import com.example.myapplication.MainActivity;
import com.example.myapplication.Myapplication;
import com.example.myapplication.R;
import com.example.myapplication.databinding.ActivityPassWordLoginBinding;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class PassWordLoginActivity extends AppCompatActivity {
    private ActivityPassWordLoginBinding binding;
    private boolean isHidePassWord = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_pass_word_login);
        click();
        initListener();
    }

    private void initListener() {
        binding.editText2.addTextChangedListener(textWatcher);
        binding.editText3.addTextChangedListener(textWatcher);
        binding.imageView27.setImageResource(R.drawable.see);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (binding.editText2.getEditableText().length() >= 1) {
                binding.imageView28.setVisibility(View.VISIBLE);
            }else {
                binding.imageView28.setVisibility(View.GONE);
            }
            if (binding.editText3.getEditableText().length() >= 1) {
                binding.imageView30.setVisibility(View.VISIBLE);
            }else {
                binding.imageView30.setVisibility(View.GONE);
            }
        }
    };

    private void click() {
        //返回主页面
        binding.imageView26.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassWordLoginActivity.this, MainActivity.class));
            }
        });

        //注册
        binding.textView15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassWordLoginActivity.this,RegisterActivity.class));
            }
        });

        //清空输入框
        binding.imageView28.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.editText2.setText("");
            }
        });
        binding.imageView30.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.editText3.setText("");
            }
        });

        // 跳转到断短信登录页面
        binding.imageView31.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassWordLoginActivity.this,LoginActivity.class));
            }
        });
        binding.textView31.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassWordLoginActivity.this,LoginActivity.class));
                PassWordLoginActivity.this.finish();
            }
        });

        //忘记密码
        binding.textView26.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassWordLoginActivity.this,FindPassWordActivity.class));
            }
        });

        //是否显示密码
        binding.imageView27.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHidePassWord) {
                    binding.imageView27.setImageResource(R.drawable.see);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    binding.editText3.setTransformationMethod(method);
                    isHidePassWord = false;
                }else {
                    binding.imageView27.setImageResource(R.drawable.notsee);
                    // 密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    binding.editText3.setTransformationMethod(method1);
                    isHidePassWord = true;
                }
                // 光标的位置
                int index = binding.editText3.getText().toString().length();
                binding.editText3.setSelection(index);
            }
        });

        //登录
        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取输入的手机号码
                String phoneNumbers = binding.editText2.getText().toString();

                Observable.create(new ObservableOnSubscribe<Boolean>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                        UserEntity userEntity = Myapplication.getInstance().getDatabase().userDao().getUserByPhoneNum(phoneNumbers);
                        if (userEntity != null) {
                            if (userEntity.getPassWord() == binding.editText3.getText().toString()) {
                                emitter.onNext(true);
                            }else {
                                Toast.makeText(PassWordLoginActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                            }
                        }else {
                            emitter.onNext(false);
                        }
                    }
                })
                .observeOn(Schedulers.io())   //为上面代码分配io线程
                .subscribeOn(AndroidSchedulers.mainThread())   //为下面的代码分配主线程
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean == true) {
                            startActivity(new Intent(PassWordLoginActivity.this,MainActivity.class));
                            PassWordLoginActivity.this.finish();
                        }else {
                            Toast.makeText(PassWordLoginActivity.this,"改用户未注册",Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });
    }
}